#!/bin/bash
# Copyright (c) 2011, bkil.hu
# This program is free software and can be distributed under the terms of
# the GNU General Public License v2,
# see COPYING for detailed licensing terms.
# created on 2011-02-22

#TODO: better compatibility with timedtv.sh

main(){
 DEBUG="1"
 local GRACE=99 #grace period in seconds

 debug "start"
 xscreensaver-command -watch |
 {
 read C X
 while true ; do
  debug "$C $X"
  case $C in
   UNBLANK)
    read C X
    ;;
   BLANK|LOCK|RUN) 
    while true; do
     debug "mencoder & grace wait"
     read -t $GRACE C X && continue 2
     for a in `pidof mencoder`;do
      grep -qz '\<tv://' /proc/$a/cmdline &&
       continue 2
     done
     break
    done
    debug "$HOME/bin/suspend.sh"
    $HOME/bin/suspend.sh
    read C X
  esac
 done
 }
}

debug(){
 [ -n "$DEBUG" ] || return
 echo "debug: $@ [`date`]" >&2
}

main "$@"
